package org.soarsky.wealthchengdu.module.home;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Handler;
import android.os.Message;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 画廊循环滑动定时器
 * 
 * @author huang
 * 
 */
public class GalleryTaskTimer {
	private static int index = 0;
	private static TimerTask task;
	private static Handler handler;

	/**
	 * 定时器，实现自动播放
	 */
	private static void tasktimer(final Gallery pictureGallery,
			final Handler handler) {
		task = new TimerTask() {
			@Override
			public void run() {
				Message message = new Message();
				index = pictureGallery.getSelectedItemPosition();
				index++;
				message.what = index;
				handler.sendMessage(message);
			}
		};
	}

	/**
	 * 启动定时器
	 * 
	 * @param pictureGallery
	 */
	public static void starttimer(Gallery pictureGallery,
			LinearLayout imgnumberliner, int down, int up,
			ImageView[] imageviews) {
		selection(pictureGallery, imgnumberliner, down, up, imageviews);
		tasktimer(pictureGallery, handler);
		Timer time = new Timer();
		time.schedule(task, 5000, 5000);
	}

	/**
	 * 设置滑动哪一个
	 * 
	 * @param pictureGallery
	 * @param imgnumberliner
	 * @param down
	 * @param up
	 * @param imageviews
	 */
	private static void selection(final Gallery pictureGallery,
			final LinearLayout imgnumberliner, final int down, final int up,
			final ImageView[] imageviews) {
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				pictureGallery.setSelection(msg.what);
				AllGalleryShow.getinstanise().GalleryclickNumber(msg.what,
						imgnumberliner, down, up, imageviews);
			}
		};
	}

	/**
	 * 结束定时器
	 */
	public static void endtimer() {
      if(task!=null)
		task.cancel();
	}
	
	/**
	 * 暂停定时器
	 */
	public static void waittimer()
	{
//		if(time!=null)
//		{
//			try {
//				time.wait();
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}
}
